#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "myline.h"
#include "mycircle.h"
#include "myrectangle.h"
#include <QTextEdit>
#include <QWheelEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_scene = new QGraphicsScene(0,0,this->width(),this->height());
    ui->graphicsView->setScene(m_scene);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete m_scene;
}

QPointF temppt;
bool isFirst=true;
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(mcurDrawType == DLine)
    {
        if(isFirst)
        {
            temppt = ui->graphicsView->mcurPt;
            isFirst = false;
        }
        else
        {
            MyLine *line = new MyLine;
            line->mspt = temppt;
            line->mept = ui->graphicsView->mcurPt;
            m_scene->addItem(line);
            isFirst = true;
        }
    }
    if(mcurDrawType == DCircle)
    {
        if(isFirst)
        {
            temppt = ui->graphicsView->mcurPt;
            isFirst = false;
        }
        else
        {
            MyCircle *circle = new MyCircle(temppt,ui->graphicsView->mcurPt);
            m_scene->addItem(circle);
            isFirst = true;
        }
    }
    if (mcurDrawType == DRectangle) { // 处理矩形绘制
        if (isFirst) {
            temppt = ui->graphicsView->mcurPt;
            isFirst = false;
        } else {
            MyRectangle *rectangle = new MyRectangle;
            rectangle->mspt = temppt;
            rectangle->mept = ui->graphicsView->mcurPt;
            m_scene->addItem(rectangle);
            m_scene->update(); // 显式更新场景
            isFirst = true;
        }
    }
}

void MainWindow::on_actionLine_triggered()
{
    mcurDrawType = DLine;
    mcurModifyType = MNone;

    // 下面的代码是为了遍历场景中的所有图元，并把它们的可选择属性都设计为false，也就是不可选择
    QList<QGraphicsItem *> allitems = m_scene->items();
    if(!allitems.isEmpty())
    {
        foreach (QGraphicsItem * i, allitems) {
            i->setFlag(QGraphicsItem::ItemIsSelectable,false);
        }
    }
}

void MainWindow::on_actionSelect_triggered()
{
    mcurDrawType = DNone;
    mcurModifyType = MSelect;

    // 下面的代码是为了遍历场景中的所有图元，并把它们的可选择属性都设计为true，也就是可选择
    QList<QGraphicsItem *> allitems = m_scene->items();
    if(!allitems.isEmpty())
    { foreach (QGraphicsItem * i, allitems) {
            i->setFlag(QGraphicsItem::ItemIsSelectable,true);
        }
    }
}

void MainWindow::on_actionCircle_triggered()
{
    mcurDrawType = DCircle;
    mcurModifyType = MNone;

    QList<QGraphicsItem *> allitems = m_scene->items();
    if(!allitems.isEmpty())
    {
        foreach (QGraphicsItem * i, allitems) {
            i->setFlag(QGraphicsItem::ItemIsSelectable,false);
        }
    }
}

void MainWindow::on_actionExit_triggered()
{
    this->close();
}

void MainWindow::on_actionNew_triggered()
{
    if (ui->textEdit->document()->isModified() || !m_scene->items().isEmpty()) {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this, "确认", "是否保存当前文件?",
                                      QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
        if (reply == QMessageBox::Yes) {
            if (currentFilePath.isEmpty()) {
                on_actionSave_as_triggered();
            } else {
                saveFile(currentFilePath);
            }
        } else if (reply == QMessageBox::Cancel) {
            return;
        }
    }

    // 实际新建操作
    ui->textEdit->clear();
    m_scene->clear(); // 清空场景
    currentFilePath.clear();
    setWindowTitle("Untitled");
}


void MainWindow::on_actionOpen_triggered()
{
    if (ui->textEdit->document()->isModified() || !m_scene->items().isEmpty()) {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::question(this, "确认", "是否保存当前文件?",
                                      QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
        if (reply == QMessageBox::Yes) {
            if (currentFilePath.isEmpty()) {
                on_actionSave_as_triggered();
            } else {
                saveFile(currentFilePath);
            }
        } else if (reply == QMessageBox::Cancel) {
            return;
        }
    }

    QString fileName = QFileDialog::getOpenFileName(this, "打开文件");
    if (!fileName.isEmpty()) {
        if (loadFile(fileName)) {
            currentFilePath = fileName;
            setWindowTitle(fileName);
        }
    }
}

void MainWindow::on_actionSave_triggered()
{
    if (currentFilePath.isEmpty()) {
        on_actionSave_as_triggered();
    } else {
        saveFile(currentFilePath);
    }
}

void MainWindow::on_actionSave_as_triggered()
{
    QString fileName = QFileDialog::getSaveFileName(this, "另存为", currentFilePath);
    if (!fileName.isEmpty()) {
        if (saveFile(fileName)) {
            currentFilePath = fileName;
            setWindowTitle(fileName);
        }
    }
}

bool MainWindow::saveFile(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, "警告", "无法保存文件: " + file.errorString());
        return false;
    }

    QTextStream out(&file);
    // 如果你同时使用文本编辑和绘图，确保保存正确的数据
    out << ui->textEdit->toPlainText();
    out.flush();
    file.close();

    ui->textEdit->document()->setModified(false);
    return true;
}

bool MainWindow::loadFile(const QString &filePath)
{
    QFile file(filePath);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
        return false;
    }

    QTextStream in(&file);
    QString text = in.readAll();
    ui->textEdit->setText(text);
    file.close();

    ui->textEdit->document()->setModified(false);
    return true;
}




void MainWindow::on_actionRectangle_triggered()
{
    mcurDrawType = DRectangle;
    mcurModifyType = MNone;

    QList<QGraphicsItem *> allitems = m_scene->items();
    if (!allitems.isEmpty()) {
        foreach (QGraphicsItem *i, allitems) {
            i->setFlag(QGraphicsItem::ItemIsSelectable, false);
        }
    }
}

